import React,{ Component } from 'react'
import PubSub from 'pubsub-js'
import { nanoid } from 'nanoid'

export default class Header extends Component{
  
  handleKeyUp = (event)=>{
    const {keyCode,target} = event
    if(keyCode != 13) return
    if(target.value.trim() === ''){
      alert('输入不能为空')
      return
    }
    const todoObj = {id:nanoid(),name:target.value,done:false}
    PubSub.publish('Header',todoObj)
  }

  render(){
    return (
      <input onKeyUp={this.handleKeyUp} type="text" placeholder="输入，按回车确定"/>
    )
  }
}